﻿using Csla;
using System;
using Csla.Serialization;
using BizControl.DalLinq;
using BizControl.Library.BusinessObjects.People;
using BizControl.Resources;
using System.Linq;

namespace BizControl.Library.BusinessObjects.Notes
{
  [Serializable()]
  public class NoteList : BusinessListBase<NoteList, NoteBO>
  {
    #region  Business Methods

    private PersonBO Person;

    public NoteBO this[NoteBO noteSearchFor]
    {
      get
      {
        foreach (NoteBO note in this)
        {
          if (note.NoteId == noteSearchFor.NoteId)
            return note;
        }
        return null;
      }
    }

    public NoteBO AddNewNote()
    {
      var note = NoteBO.NewNote(Person);
      this.Add(note);
      return note;
    }

    public void Remove(Guid noteId)
    {
      foreach (NoteBO note in this)
      {
        if (note.NoteId == noteId)
        {
          Remove(note);
          break;
        }
      }
    }

    public bool Contains(Guid noteId)
    {
      foreach (NoteBO note in this)
        if (note.NoteId == noteId)
          return true;
      return false;
    }

    public bool ContainsDeleted(Guid noteId)
    {
      foreach (NoteBO note in DeletedList)
        if (note.NoteId == noteId)
          return true;
      return false;
    }

    #endregion

    #region  Factory Methods

    internal static NoteList NewNotes(PersonBO person)
    {
      return DataPortal.CreateChild<NoteList>(person);
    }

    internal static NoteList GetNotes(Note[] data, PersonBO person)
    {
      return DataPortal.FetchChild<NoteList>(data, person);
    }

    private NoteList()
    { /* require use of factory methods */ }

    #endregion

    #region  Data Access

    private void Child_Fetch(Note[] data, PersonBO person)
    {
      if (person == null) throw new ArgumentNullException(ExceptionsMessages.PersonNullReference);
      Person = person;
      if (data == null) return;
      this.RaiseListChangedEvents = false;
      foreach (var child in data)
        Add(NoteBO.GetNote(child));
      this.RaiseListChangedEvents = true;
    }

    #endregion

  }
}